/**
 * Copyright 2005 SIB Framework

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package org.sibframework.sda;

import java.util.HashMap;
import java.util.Map;

/**
 * registry to Used supply table provider.
 * @author Spring Song
 *
 */
public class TableRegister {
	/**
	 * 
	 */
	private static Map<Class<Object>, TableProvider> tableRegisters = new HashMap<Class<Object>, TableProvider>();
	static {

		/**
		 * register DB table to java object.
		 * 
		 * Example
		register(Answer.class, new TableProvider(){
			public String getTableName() {
				return "answer";
			}
			public String getPrimaryKey() {
				return "answer_id";
			}});*/


	}
	/**
	 * find a table provider for a give object.
	 * @param object
	 * @return  TableProvider object.
	 */
	public static TableProvider getTableProvider(Object object){
		return getTableProvider( object.getClass());
	}
	/**
	 * find a table provider for a give class.
	 * @param objectClass
	 * @return TableProvider object.
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static TableProvider getTableProvider(Class objectClass){
		Object o = tableRegisters.get(objectClass);
		if (o != null && o instanceof TableProvider) {
			return (TableProvider) o;
		}
		IntelligentTableProvider rtp = new IntelligentTableProvider(objectClass);
		register(objectClass, rtp);
		return rtp;
	}
	
	
	/**
	 * register a new table provider.
	 * @param classe
	 * @param tp
	 */
	public static void register(Class<Object> classe, TableProvider tp) {
		tableRegisters.put(classe, tp);
	}
	
	/**
	 * clear the table registry.
	 */
	public static void clear() {
		tableRegisters.clear();
	}
}
